package com.zgc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zgc.common.R;
import com.zgc.entity.RoleMenu;
import com.zgc.service.RoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/roleMenu")
public class roleMenuController {
    @Autowired
    private RoleMenuService roleMenuService;
    //回填，被选中的 根据角色id获取菜单列表
    @GetMapping("/{roleid}")
    public R getMenusByRoleid(@PathVariable String roleid){
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper();
//添加排序条件
        queryWrapper.eq(RoleMenu::getRoleId,roleid);
        List<RoleMenu> list= roleMenuService.list(queryWrapper);
        List<String> menuids=new ArrayList<>();
        for (RoleMenu roleMenu:list) {
            menuids.add(String.valueOf(roleMenu.getMenuId()));
        }
        return R.success(menuids);
    }
    //点击确定，配置菜单信息
    @PostMapping("/save")
    public R<String> save(@RequestBody Map hashmap) {
        String roleid= (String) hashmap.get("roleid");
        ArrayList list= (ArrayList) hashmap.get("menuids");
        roleMenuService.saveAll(roleid,list);
        return R.success("菜单配置成功");
    }
}
